###################################################################################################
#FigureA_4_PanelA
#This figure plots the evolution of the average value of commodities disbursed from January to November of 2017

###################################################################################################

#############################################################
## Set up
#############################################################

#SET COLORS
fColor <- "black"
mColor <- "gray60"
lightColor <- "#97B8DE"
darkColor <- "#4669BD"

#reconciled and unreconciled strings
ration.value_total <- c("totalr","totalnr")
ration.name_total <- c("total reconciled","total non-reconciled")


month.list <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug","Sep","Oct", "Nov")

#Plot format
rect_r <- data.frame(xmin=7, xmax=10, 
                     ymin=-Inf, ymax=Inf)

fontSize <- 16
theme_set(theme_gray(base_size = fontSize))


############################################################
##Non-reconciled time series
############################################################
tmp.outcome <- "value"


tmp.ration <- ration.value_total[2]
tmp.ration1 <- ration.name_total[2]

tmp.data <- foreign::read.dta(paste(AdminDataDir,tmp.ration,"_",tmp.outcome,"_meandisbursement_reconciliation_time_series.dta",sep = ""))

meltdf <- reshape2::melt(tmp.data[,c("month",paste("dis_",tmp.outcome,"_perRC_",tmp.ration,sep = ""),"yhat")],id="month")
meltdf$group <- NA
meltdf$group[meltdf$variable == paste("dis_",tmp.outcome,"_perRC_",tmp.ration,sep = "")] <- "Observed"
meltdf$group[meltdf$variable =="yhat"] <- "Fitted value"

meltdf.se <- reshape2::melt(tmp.data[,c("month",paste("dis_",tmp.outcome,"_perRC_",tmp.ration,"_se",sep = ""),"yhat_se")],id="month")
meltdf.se$se <- 0
meltdf.se$se[meltdf.se$variable =="yhat_se"] <- meltdf.se$value[meltdf.se$variable =="yhat_se"]
meltdf.se$value <- NULL
meltdf.se$group <- NA
meltdf.se$group[meltdf.se$variable == paste("dis_",tmp.outcome,"_perRC_",tmp.ration,"_se",sep = "")] <- "Observed"
meltdf.se$group[meltdf.se$variable =="yhat_se"] <- "Fitted value"
meltdf.se$variable <- NULL


plot.dat2 <- merge(meltdf,meltdf.se, by = c("month","group"))
plot.dat2$lwr <- plot.dat2$value - 1.96*plot.dat2$se
plot.dat2$upr <- plot.dat2$value + 1.96*plot.dat2$se

############################################################
##Reconciled time series
############################################################
tmp.outcome <- "value"

tmp.ration <- ration.value_total[1]
tmp.ration1 <- ration.name_total[1]

tmp.data <- foreign::read.dta(paste(AdminDataDir,tmp.ration,"_",tmp.outcome,"_meandisbursement_reconciliation_time_series.dta",sep = ""))

meltdf <- reshape2::melt(tmp.data[,c("month",paste("dis_",tmp.outcome,"_perRC_",tmp.ration,sep = ""),"yhat")],id="month")
meltdf$group <- NA
meltdf$group[meltdf$variable == paste("dis_",tmp.outcome,"_perRC_",tmp.ration,sep = "")] <- "Observed"
meltdf$group[meltdf$variable =="yhat"] <- "Fitted value"

meltdf.se <- reshape2::melt(tmp.data[,c("month",paste("dis_",tmp.outcome,"_perRC_",tmp.ration,"_se",sep = ""),"yhat_se")],id="month")
meltdf.se$se <- 0
meltdf.se$se[meltdf.se$variable =="yhat_se"] <- meltdf.se$value[meltdf.se$variable =="yhat_se"]
meltdf.se$value <- NULL
meltdf.se$group <- NA
meltdf.se$group[meltdf.se$variable == paste("dis_",tmp.outcome,"_perRC_",tmp.ration,"_se",sep = "")] <- "Observed"
meltdf.se$group[meltdf.se$variable =="yhat_se"] <- "Fitted value"
meltdf.se$variable <- NULL


plot.dat1 <- merge(meltdf,meltdf.se, by = c("month","group"))
plot.dat1$lwr <- plot.dat1$value - 1.96*plot.dat1$se
plot.dat1$upr <- plot.dat1$value + 1.96*plot.dat1$se

############################################################
##Combining the plots
############################################################
plot.dat2$group <- paste(plot.dat2$group,"non-reconciled")
plot.dat1$group <- paste(plot.dat1$group,"reconciled")
plot.dat <- rbind(plot.dat1,plot.dat2)

##Plot 
ggplot(plot.dat, aes(x=month, y=value,group = group, colour= group,linetype = group)) +
  geom_line(size= 1.5)+
  geom_ribbon(aes(ymin=lwr,ymax=upr), alpha = 0.2, colour=NA) + 
  scale_colour_manual(values=c(fColor,darkColor,mColor,lightColor)) +
  scale_linetype_manual(values=c("solid","solid","dotted","dotted")) +
  xlab("Month") + ylab(paste("Average disbursement value per rationcard")) +     
  guides(colour = guide_legend(override.aes = list(shape = NA))) + #remove legend title
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.text=element_text(size=16),
        axis.title=element_text(size=16),legend.position="none") +
  scale_y_continuous(limit=c(0,550),breaks = seq(0,550,100)) +
  scale_x_continuous(limit=c(1,11), breaks = seq(1,11,1), labels = month.list) +
  geom_rect(data=rect_r, aes(xmin=xmin, xmax=xmax, 
                             ymin=ymin, ymax=ymax), 
            color="gray100", alpha=0.05, inherit.aes = F,
            linetype = 0)


ggsave(file=paste(OutputDir,  "FigureA_4_PanelA.pdf",sep=""), width=12, height=6)






